package monteCarloSingle;

import java.util.Random;

public class MonteCarloSimulation {

	/**
	 * One simulation
	 * @param CallPutFlag
	 * @param S
	 * @param X
	 * @param T
	 * @param r
	 * @param b
	 * @param v
	 * @param nSteps
	 * @return
	 */
	public static double MonteCarloStandardOption(String CallPutFlag, double S,
			double X, double T, double r, double b, double v, int nSteps) {
		
		double dt,st,sum=0, drift, vSqrdt;
		int j,z=0;
		Random rand = new Random();
		
		dt = T/nSteps;
		drift = (b - Math.pow(v, 2) /2) * dt;
		vSqrdt = v * Math.sqrt(dt);
		
		if(CallPutFlag.equals("c")){
			z=1;
		}
		else if(CallPutFlag.equals("p")){
			z=-1;
		}
		
			st = S;
			for(j=1;j<= nSteps;j++){
				st = st * Math.exp(drift + vSqrdt * rand.nextGaussian());
			}
			sum = sum + Math.max(z*(st-X), 0);
	
			return Math.exp(-r * T)* (sum);
	}
	
	/**
	 * Average of all done simulations 
	 * @param tab
	 * @return
	 */
	public static double Average(double[] tab){
		double result=0;
		for(int i=0;i< tab.length;i++){
			result += tab[i];
		}
		return result/tab.length;
}
}